//
//  Day03ViewController.swift
//  SwiftDemo
//
//  Created by zhudeshuai on 2018/9/27.
//  Copyright © 2018年 zhudeshuai. All rights reserved.
//

import Foundation
import UIKit
import CoreLocation
class Day03ViewController: BaseViewController, CLLocationManagerDelegate {
    
    let locationManager = CLLocationManager()
    let geocoder = CLGeocoder()
    let show = UILabel()
    override func viewDidLoad() {
        super.viewDidLoad()
        let bgView = UIImageView()
        bgView.image = UIImage(named: "phoneBg.jpeg")
        bgView.backgroundColor = UIColor.cyan
        contentView.addSubview(bgView)
        bgView.snp.makeConstraints{(make) in
            make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0))
//            make.edges.equalTo(contentView).inset(UIEdgeInsetsMake(0, 0, 0, 0))
        }
        locationManager.delegate = self
        //显示label
        show.text = "等待定位"
        show.font = UIFont.systemFont(ofSize: 20)
        show.textColor = UIColor.white
        contentView.addSubview(show)
        show.snp.makeConstraints{(make) in
            make.centerX.equalTo(contentView)
            make.top.equalToSuperview().offset(50)
        }
        
        
        //按钮
        let click = UIButton()
        click.setTitle("定位", for: .normal)
        click.setTitleColor(UIColor.white, for: .normal)
        click.layer.borderColor = UIColor.white.cgColor
        click.layer.borderWidth = 1
        click.layer.cornerRadius = 5
        
        click.contentEdgeInsets = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
        contentView.addSubview(click)
        click.snp.makeConstraints{(make) in
            make.bottom.equalTo(contentView).offset(-50)
            make.centerX.equalTo(contentView)
            
        }
        click.addTarget(self, action: #selector(location), for: .touchUpInside)
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locations = locations as NSArray
        let currentLocation = locations.lastObject as! CLLocation
        let locationStr = "lat:\(currentLocation.coordinate.latitude) lng:\(currentLocation.coordinate.longitude)"
        show.text = locationStr
        reverseGeocode(location:currentLocation)
        locationManager.stopUpdatingLocation()
    }
    
    //转换经纬度成地址
    func reverseGeocode(location: CLLocation) {
        geocoder.reverseGeocodeLocation(location, completionHandler: {(placeMark, error) in
            if(error == nil) {
                let tempArray = placeMark! as NSArray
                let mark = tempArray.firstObject as! CLPlacemark
                
                let addressDictionary = mark.addressDictionary! as NSDictionary
                let country = addressDictionary.value(forKey: "Country") as! String
                let city = addressDictionary.object(forKey: "City") as! String
                let street = addressDictionary.object(forKey: "Street") as! String
                
                let finalAddress = "\(street),\(city),\(country)"
                self.show.text = finalAddress
            }
        })
    }
    
    @objc func location() {
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }
}
